﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpTrainer.Util;
using Zeus.Thunder.Core;

namespace SharpTrainer.Delegates
{
    class LinqLamdaTest : ITestCase
    {
        public void Run()
        {
            var input = new List<int> { 8, 3, 5, 1, 2, 7, 4 };
            var result = new List<int>();

            PerformanceManager perf;
            
            perf = new PerformanceManager("Old Way Performance", new TimeConsole());
            perf.Start();

            //old way
            foreach (int x in input)
            {
                if (x % 2 != 0)
                {
                    result.Add(x + 1);
                }
            }
            Console.WriteLine("Old Way:");
            foreach (int x in result)
            {
                Console.Write("{0} ", x);
            }

            Console.WriteLine();

            perf.Stop();
            perf.PrintTime();

            Console.WriteLine();

            perf = new PerformanceManager("Linq Lamda Performance", new TimeConsole());
            perf.Start();

            var res = input.Where(x => x % 2 != 0)  // filter on predicate
                           .OrderBy(x => x)
                           .Select(x => x + 1);    // map using projection
            Console.WriteLine("Linq:");
            Display(res);

            perf.Stop();
            perf.PrintTime();

            Console.WriteLine();

            perf = new PerformanceManager("Linq Performance", new TimeConsole());
            perf.Start();

            //This way is faster than the second one
            var resLinq = from x in input
                          where x % 2 != 0
                          orderby x
                          select x + 1;
            Display(res);

            perf.Stop();
            perf.PrintTime();
        }

        private void Display(IEnumerable<int> lst)
        {
            GenericUtil<int>.PrintEnumerable(lst);
        }

    }
}
